ios 跑马灯

swift3 可将time 换成GCD 优化 使用过程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/// 跑马灯View 需要手动调用清理函数
class MarqueeView: UIView {
var iconImageView:UIImageView!
var titleLabel:UILabel!
var showLabel:UILabel!
/// 显示的跑马灯数组
var showTextArray:[String] = ["测试跑马灯1","测试跑马灯2","测试跑马灯3","测试跑马灯4"]
/// 显示到的位置
var showTextCount = 0
var showtime:Timer?
override init(frame: CGRect) {
super.init(frame: frame)
let icon_W = frame.height*0.4444
self.iconImageView = UIImageView(frame: CGRect(x: interval_W_20, y: 0, width: icon_W, height: icon_W))
self.iconImageView.frame.origin.y = frame.height * 0.2777
self.addSubview(self.iconImageView)
self.titleLabel = UILabel()
self.titleLabel.font = GlobalFont_32
self.titleLabel.textColor = GlobalMainToneColor_2_4
self.addSubview(self.titleLabel)
self.showLabel = UILabel()
self.showLabel.font = GlobalFont_32
self.showLabel.textColor = GlobalTxtColor_1
self.addSubview(self.showLabel)
self.layer.masksToBounds = true
}
func setData(icon:UIImage,title:String,showTextArr:[String],backColor:UIColor = UIColor.white) {
self.backgroundColor = backColor
self.iconImageView.image = icon
self.titleLabel.text = title.truncate(start: 0, end: 8)
self.titleLabel.sizeToFit()
self.titleLabel.frame.origin.x = self.iconImageView.frame.origin.x+self.iconImageView.frame.width+interval_W_20
self.titleLabel.center.y = self.iconImageView.center.y
self.showLabel.frame.origin.x = self.titleLabel.frame.origin.x+self.titleLabel.frame.width + interval_W_20
if self.showTextArray != showTextArr{
self.showTextArray = showTextArr
self.showTextCount = 0
}
if showtime != nil{
showtime!.invalidate()
showtime = nil
}
self.timeChange()
self.showtime = Timer.scheduledTimer(timeInterval: timeInterval, target: self, selector: #selector(self.timeChange), userInfo: nil, repeats: true)
}
deinit {
printLog("deinit MarqueeView")
}
func clear(){
self.stopTime()
}
/// 必须手动调用
private func stopTime(){
if showtime != nil{
showtime!.invalidate()
showtime = nil
}
}
/// 时间间隔
let timeInterval:Double = 5
/// 时间到处理函数
func timeChange(){
if self.showTextCount < self.showTextArray.count{
self.showLabel.text = self.showTextArray[self.showTextCount]
self.showTextCount += 1
}else{
self.showTextCount = 0
self.showLabel.text = self.showTextArray[self.showTextCount]
}
self.showLabel.sizeToFit()
var frame = self.showLabel.frame
frame.origin.y = self.frame.height
self.showLabel.frame = frame
UIView.beginAnimations("showText", context: nil)
UIView.setAnimationDuration(timeInterval)
UIView.setAnimationCurve(UIViewAnimationCurve.linear)
UIView.setAnimationDelegate(self)
UIView.setAnimationRepeatAutoreverses(false)
UIView.setAnimationRepeatCount(0)
frame = self.showLabel.frame
frame.origin.y = -frame.size.height
self.showLabel.frame = frame
UIView.commitAnimations()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
Author

陈昭

Posted on

2017-02-08

Updated on

2021-12-27

Licensed under

You need to set install_url to use ShareThis. Please set it in _config.yml.
You forgot to set the business or currency_code for Paypal. Please set it in _config.yml.

Kommentare

You forgot to set the shortname for Disqus. Please set it in _config.yml.